package com.booklove.properties;

import io.jsonwebtoken.io.Decoders;
import io.jsonwebtoken.security.Keys;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.crypto.SecretKey;

@Component
@ConfigurationProperties(prefix = "books.jwt")
@Data
public class JwtProperties {

    private static final Logger log = LoggerFactory.getLogger(JwtProperties.class);
    /**
     * 用户端微信用户生成jwt令牌相关配置
     */
    private String userSecretKeyString;
    private long userTtl;
    private String userTokenName;
    private SecretKey userSecretKey;

    @PostConstruct
    public void init() {
        byte[] keyBytes = Decoders.BASE64.decode(userSecretKeyString);
        userSecretKey = Keys.hmacShaKeyFor(keyBytes);
        log.info("生成密钥中 -> userSecretKeyString: {}", userSecretKeyString);
    }


}
